package ch.zbw.timeline.presentation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ch.zbw.timeline.domain.Role;
import ch.zbw.timeline.presentation.ClientEventListener;

/**
 * Login window
 * 
 * @author Daniel Stefanovic
 * @version 18.01.2012
 * 
 */
public class LoginClientView extends JFrame {

	private JLabel lblUserName = new JLabel("Benutzername");
	private JLabel lblPassword = new JLabel("Passwort");
	private JLabel lblRole = new JLabel("Anmelden als");
	private JTextField txtUserName = new JTextField();
	private JPasswordField txtPassword = new JPasswordField();
	private JComboBox cmbRoles = new JComboBox();
	private JButton btnLogin = new JButton("Anmelden");
	private List<Role> roles;

	/**
	 * Constructor
	 */
	public LoginClientView() {
		init();
	}

	/**
	 * Initialization
	 */
	private void init() {
		// Set position and size of the components
		lblUserName.setBounds(15, 15, 120, 25);
		lblPassword.setBounds(15, 45, 120, 25);
		lblRole.setBounds(15, 75, 120, 25);
		txtUserName.setBounds(130, 15, 200, 25);
		txtPassword.setBounds(130, 45, 200, 25);
		cmbRoles.setBounds(130, 75, 200, 25);
		btnLogin.setBounds(230, 110, 100, 25);

		// add components to window
		this.add(lblUserName);
		this.add(lblPassword);
		this.add(lblRole);
		this.add(txtUserName);
		this.add(txtPassword);
		this.add(cmbRoles);
		this.add(btnLogin);

		this.setTitle("timeLine - Anmeldung");
		this.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
		this.setSize(360, 180);
		this.setResizable(false);
		this.setLayout(null);
		this.setLocationRelativeTo(null);
	}

	/**
	 * Add an event listener
	 * 
	 * @param listener event listener
	 */
	public void addEventListener(final ClientEventListener listener) {
		btnLogin.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				listener.loginButtonPressed();
			}
		});

		// react to enter key
		txtUserName.addKeyListener(new EnterKeyListener(listener));
		txtPassword.addKeyListener(new EnterKeyListener(listener));
		cmbRoles.addKeyListener(new EnterKeyListener(listener));
	}

	/**
	 * Set the roles
	 * 
	 * @param roles list of roles
	 */
	public void setRoles(List<Role> roles) {
		this.roles = roles;

		for (Role role : roles) {
			cmbRoles.addItem(role.getDescription());
		}
	}

	/**
	 * Get the user name
	 * 
	 * @return user name
	 */
	public String getUserName() {
		return txtUserName.getText();
	}

	/**
	 * Get the password
	 * 
	 * @return password
	 */
	public String getPassword() {
		return new String(txtPassword.getPassword());
	}

	/**
	 * Get the selected role
	 * 
	 * @return selected role
	 */
	public Role getRole() {
		int index = cmbRoles.getSelectedIndex();
		return roles.get(index);
	}

	/**
	 * Fires login method if enter pressed
	 */
	class EnterKeyListener implements KeyListener {

		private ClientEventListener listener;

		public EnterKeyListener(ClientEventListener listener) {
			this.listener = listener;
		}

		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();

			// enter pressed?
			if (key == KeyEvent.VK_ENTER) {
				listener.loginButtonPressed();
			}
		}

		public void keyReleased(KeyEvent e) {
		}

		public void keyTyped(KeyEvent e) {
		}
	}
}
